global root_dir = "`1'"

include "$root_dir/code/config/config.do"


cap noi log using ${log_dir}/Construct_minimumwages_GER_US_UK_IE.log, replace name(dat)

*Handle empty arguments
global arg1 = cond("`2'" == "___EMPTY___", "", "`2'")
global arg2 = cond("`3'" == "___EMPTY___", "", "`3'")
global arg3 = cond("`4'" == "___EMPTY___", "", "`4'")
global arg4 = cond("`5'" == "___EMPTY___", "", "`5'")

if "$arg1" != "" {
    global weight_category "$arg1"
    di "Weight category: ${weight_category}"
}

if "$arg2" != "" {
    global weight_versions "$arg2"
    di "Weight versions: ${weight_versions}"
}

if "$arg3" != "" {
    global weight_window "$arg3"
    di "Weight window: ${weight_window}"
}

if "$arg4" != "" {
	global wtype "$arg4"
}
di "${wtype}"
capture noi {



*******************************************************************************
***************** 	MINIMUM WAGES 					 **************************
*******************************************************************************

* We create a file that contains the minimum wages from as many countries 
* as possible. There are some things to consider.

* a) Germany did not have a mandated federal minimum wage until 2015. We will 
* instead follow previous literature (Dolado et al 1996) and use the collectively 
* bargained industry specific minimum wages which in effect constitute law once
* they have been negotiated. These exist for "production industries" back to the 
* 1950s. Similar things are true for a few smaller countries (i.e. Denmark) but 
* we will not do that now. These data come from correspondance with Sabine Lenz and 
* Thomas Körner at the Statistical Agency of Germany (destatis.de). 

* b) The US minimum wage based on minimum wage and employment data from the St. Louis FED
* we correct some measures fo certain states

* c) The UK had wages councils from 1909 to 1993. These were industry specific

* d)  Ireland has no minimum wage data before 2000. We impute
*    it backwards based on the minimum wage of 2000 and the wage wage growth 
*    agreed on by unions, government and employer organizations in Irelands 
*    "Social Partnership" agreements. The two relevant ones for the period 
*    from 1995-1999 are the Program for Competitiveness and Work (PCW) 
*    from 1993-1996 and the Partnership 2000 from 1997-2000.
*     For more information see "The Irish social partnership and 
*    the "celtic tiger" phenomenon" (Baccaro L. and Simoni M.
*    2004); International Institute for Labour Studies.

*The US and Ireland we calculate directly on an hourly basis. 
*The UKs are weekly wages, which we tarnsform later on once we merge.
*Germany is simply an index, which we combine with an official wage once
* merged with the OECD data to get the actual wage.


**************************************************************************
****GERMAN MINIMUM WAGES ************************************************
**************************************************************************

* Index of monthly wages from 1980-1994. 
*Lenz, Sabine, and Thomas Körner. 2017. "Index for Bargaining Agreements on Wages in Germany", Destatis, Unpublished data.
*Import data. 
import excel ${mow_data_raw}/Minimum_wages/Tarifindizes_1958_2003.xlsx, sheet("1995=100") cellrange(A11:C56) clear

* Rearrange and clean data. 
drop B
destring A, replace
rename A year
rename C min_wages_index
*The two datasets overlap from 1995 to 2003.
drop if year>=1995
* Let's just leave this out.  	
drop if year < 1980 		
tempfile GER_minw_index8094
save `GER_minw_index8094'


* 1995 - 2016. Index on German minimum wages | Destatis | www.destatis.de/DE/Publikationen/Thematisch/VerdiensteArbeitskosten/Tarifverdienste/TarifverdienstLangeReiheXLS_5622203.xlsx?__blob=publicationFile |
		
* Ratio of the two dataset is 1.428(1995)  and 1.424 (2003) so deemed identical. 
* Renormalize to 100 in 1995.
*Import data. 
import excel ${mow_data_raw}/Minimum_wages/TarifverdienstLangeReiheXLS_5622203.xlsx, sheet("D-Std-Jahr") cellrange(A9:G30) clear

* Rearange and clean data. 
keep B G
rename B year
rename G min_wages_index
drop if year==.
*Renormalize to 100 in 1995. 
gen min_wages_index_temp=min_wages_index if year==1995
replace min_wages_index_temp=sum(min_wages_index_temp)
replace min_wages_index=min_wages_index/min_wages_index_temp*100


*Merge with the dataset - 1980-1994(from above). 
append using `GER_minw_index8094'

*Rearrange and clean data. 
drop min_wages_index_temp
rename min_wages_index min_wage
sort year
gen country="Germany"
save ${mow_data_proc}/Germany_minw_index.dta, replace

*
rename min_wage min_wage_germany
save ${mow_data_proc}/Germany_minw.dta, replace


**************************************************************************
***		UK MINIMUM WAGES - WAGES COUNCILS  *******************************
**************************************************************************

* From 1909 to 1993 the UK had wages councils set up to regulate the minimum
* wage across a number of different industries. Primarily in hotel, service, 
* and very low-wage manufacturing (Dickens and Machin and Manning, 1999).
* These we will link with the official minimum wage that the UK has had in place 
* since 1999 (OECD) data. During the period 1994-1998 we keep the nominal level 
* of 1993. 

*For privacy reasons, we provide a already processed version of the data , aggregated as employment weighted minimum wages.
*For the code to reduce the dataset we received please see the file reduce_UK_minw.do
*We thank Richard Dickens, Alan Manning and S.J, Machin 

**************************************************************************
***		Bringing UK into the correct form    *****************************
**************************************************************************
*Minimum wages | OECD.Stat | Downloaded October 19th 2017.  Here just used for its structure
import excel ${mow_data_raw}/Minimum_wages/minimum_wages.xlsx, sheet("OECD.Stat export") cellrange(A5:AO40) clear allstring
drop if A=="Country"
drop if A=="Data extracted on 19 Oct 2017 13:18 UTC (GMT) from OECD.Stat"

* These two countries we don't use: Germany only has federal wage for a few -> done above
* years. USA is federal wages.

*Reshape data. 
drop B C AO

rename D y1980
rename E y1981
rename F y1982
rename G y1983
rename H y1984
rename I y1985
rename J y1986
rename K y1987
rename L y1988
rename M y1989
rename N y1990
rename O y1991 
rename P y1992
rename Q y1993
rename R y1994
rename S y1995
rename T y1996
rename U y1997
rename V y1998
rename W y1999
rename X y2000
rename Y y2001
rename Z y2002
rename AA y2003
rename AB y2004
rename AC y2005
rename AD y2006
rename AE y2007
rename AF y2008
rename AG y2009
rename AH y2010
rename AI y2011
rename AJ y2012
rename AK y2013
rename AL y2014
rename AM y2015
rename AN y2016

reshape long y, i(A) j(year) 

rename A country 
rename y min_wage
replace min_wage="" if min_wage==".."
destring min_wage, replace

*merge in UK
append using ${mow_data_raw}/Minimum_wages/nes_eg_data7594.dta, generate(UK)
sort country year

* Attach the two dataseries on the United Kingdom: The one we have from wages councils must be weekly. So we multiply by 50 
replace min_wage=min_wage*50 if UK==1
sort country year
*Drop UK observations from OECD before 1994
drop if year<=1994 & UK==0 & country=="United Kingdom"

*During the period 1994-1998 we keep the nominal level of 1993.
replace min_wage=min_wage[_n-1] if year>=1994 & year<1999 & country=="United Kingdom"
* In the UK the minimum wage started April 1999 but had been known in advance. We might as well use that in 1999. 
* The rate was 3.60 per hour and 3.70 in 2000.
* Consequently: 
replace min_wage=min_wage[_n+1]*3.60/3.7 if year==1999 & country=="United Kingdom"
drop UK
keep if country == "United Kingdom"

save ${mow_data_proc}/UK_minw.dta, replace



*******************************************************************************
***************** 	MINIMUM WAGES HOURLY			 **************************
*******************************************************************************

*Ireland and the US we construct directly at the hourly level.

**************************************************************************
********US MINIMUM WAGES - FEDERAL AND STATE  *****************************
**************************************************************************

*We will here generate US minimum wages on the US level by using state
*minimum wages. For this purpose we will use the employment in 1995 till 2009 in 
*manufacturing in each state as a weight to compute the total US
*minimum wage from the state minimum wages. 

*Import the employment 1995 till 2009, 
* US employment by state | The format of the table has been discontinued by the BLS, but it is possible 
* to download the same data state-by-state from FRED (the link for e.g. Ohio is https://fred.stlouisfed.org/series/OHMFGN) | Downloaded on August 9th 2019.
import excel ${mow_data_raw}/Minimum_wages/SeriesReport-20190809112029_5f44fb.xlsx, sheet("BLS Data Series") cellrange(A4:FY57) firstrow clear

*Generate the average employment for each year over the months
foreach time in 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009{
    egen emp_`time' = rowmean(Jan`time' Feb`time' Mar`time' Apr`time' May`time' Jun`time' Jul`time' Aug`time' Sep`time' Oct`time' Nov`time' Dec`time')
}

drop Jan* Feb* Mar* Apr* May* Jun* Jul* Aug* Sep* Oct* Nov* Dec*

reshape long emp_, i(State) j(year)
rename emp_ emp

rename State auth_level

*Generate the employment share by year
egen tot_emp = total(emp), by(year)
gen emp_share = emp /tot_emp
drop emp tot_emp


save ${mow_data_proc}/employment_share.dta, replace

*State and Federal minimum wages | FRED | https://fred.stlouisfed.org/categories/27281, 
*and the states added together and cleaned. (for each state select State Minimum Wage Rate) 
use ${mow_data_raw}/Minimum_wages/fred_data.dta, clear

*First replace the minimum wage equal to the Federal minimum wage if the
*state minimum wage is smaller than the Federal one or if there is no 
*state minimum wage. Replace by hand.

replace min_wage_1 =4.38 if year==1996 & auth_level=="Federal (FLSA)"
replace min_wage_1 =4.88 if year==1997 & auth_level=="Federal (FLSA)"
replace min_wage_1 =5.46 if year==2007 & auth_level=="Federal (FLSA)"
replace min_wage_1 =6.16 if year==2008 & auth_level=="Federal (FLSA)"
replace min_wage_1 =6.86 if year==2009 & auth_level=="Federal (FLSA)"


gen federal= min_wage_1 if auth_level=="Federal (FLSA)"
egen federal2 = sum(federal) ,by(year)

replace min_wage_1 =federal2 if min_wage_1 <federal2
replace min_wage_1 =federal2 if min_wage_1 ==.

drop federal*

*Merge with the employment share 
merge 1:1 auth_level year using ${mow_data_proc}/employment_share.dta
drop if year <1995
drop if year>2009
drop if _merge==1
drop _merge
drop if auth_level=="U.S. Virgin Islands"
drop if auth_level=="Puerto Rico"


*Generate the US level minimum wage
gen temp1 = emp_share*min_wage_1
egen temp2= sum(temp1), by (year)
egen temp3=sum(emp_share), by(year)
gen hourly_usa= temp2/temp3


duplicates drop year, force
drop auth_level min_wage_1 emp_share temp*
gen country = "usa"

save ${mow_data_proc}/US_minw.dta, replace




**************************************************************************
********Ireland MINIMUM Hourly WAGES - Social Partnership  ***************
**************************************************************************

*Ireland has a minimum wage only after 2000. We calculate backwards based on agreements between unions, employers and the government.
*Note that this roughly concides with the indtroduction of the Euro in Ireland, so we make sure to convert the wages to IEP. before 2000.
*See celtic tiger paper (Baccaro L. and Simoni M., 2004) for more details

* Hourly minimum wages | OECD iLibrary | Downloaded June 21st 2019
import excel ${mow_data_raw}\Minimum_wages\minimum_wages_hours.xlsx, sheet("OECD.Stat export") cellrange(A4:AF39) firstrow clear
drop if _n==35
drop if _n==1
drop B
reshape long y, i(Time) j(year)
rename Time country
drop C 
rename y min_wage
destring min_wage, replace force
keep if country=="Ireland"

*place value for y2000 everywhere 
egen min_wage_2000 = mean(min_wage)  if year == 2000
egen mean_2000 = mean(min_wage) if year == 2000
su mean_2000, meanonly
local mean_value = r(mean)
replace min_wage_2000 = `mean_value'
drop mean_2000
*use the public euro/IEP exchange rate from 2000 to convert to IEP
replace min_wage_2000 = min_wage_2000/0.787564


*calculate backwards using Celtic Tiger wage growth figures, final numbers are the beginning of the year wages, even if year is split into 2 parts for consistency (applies only to 1999 and 1996)
*increases yoy in min wage from 1995-1999 (see table 1 of Baccaro L. and Simoni M., 2004, page 26)
*for the precise timing see the text of the Partnership 2000 agreement at http:*edepositireland.ie/bitstream/handle/2262/76830/Partnership2000.pdf?sequence=1&isAllowed=y

* 1999-2000: 1% increase in september 99, and 1.5% on jan 1 1999. we assume minimum wage starting jan 1st 2000, 

replace min_wage_2000 = min_wage_2000/(1.01*1.015) if year <= 1999
replace min_wage_2000 = min_wage_2000/1.0225 if year <= 1998
replace min_wage_2000 = min_wage_2000/1.025 if year <= 1997


*the 2.5 increase on full base pay technically only applied the last 3 months of the year, but for the first 200£ of weekly pay there was a 2.5% increase for the whole year before that. 
*assuming minimum wage employees where affected by this as well allows us to extend that to the whole year. Note that the 2.5% is pharsed as "Phase 1: 2.5% of the first £200 of basic weekly pay (£10,436 in
*annual terms) for the first 9 months of this Agreement, at the end of which the increase of 2.5% shall apply to the balance of full basic pay for the next 3 months" in the agreement, meaning there is no twostep increase for
*workers earning less than 200£ per week.
replace min_wage_2000 = min_wage_2000/(1.01*1.025) if year <= 1996

*1996 is split into two increases 1% for the second half of the year and 2.5% for the first 6 months
replace min_wage_2000 = min_wage_2000/1.025 if year <= 1995
replace min_wage_2000 = . if year <= 1994
mmerge year using ${mow_data_proc}/ieprates.dta, unmatched(master)

*fix exchange rate after 2000
replace XEU = 0.787564 if year >= 1999
gen min_wage_2000v = min_wage_2000*XEU
replace min_wage = min_wage_2000v if year <= 1999

drop min_wage_2000 min_wage_2000v XEU _merge

*preliminarily dropping everything after 2000, we merge back into oecd later on
drop if year >= 2000

ren min_wage minwage_IE
save ${mow_data_proc}/minwage_hourly_IE.dta, replace


}
if _rc == 0 {
    display "Execution finished successfully."
}
else {
    display "Execution finished with errors."
}

cap log close dat